
ifeq ($(PARAM_FILE), )
	PARAM_FILE:=../../../../Makefile.param
	include $(PARAM_FILE)
endif
ifeq ($(COMP_PARAM_FILE), )
    COMP_PARAM_FILE:=../../../../Makefile.param
    include $(COMP_PARAM_FILE)
endif

CAMERA_PATH := $(SENSOR_ROOT)/camera
LIB_PATH = $(CAMERA_PATH)/user/lib/
OBJ_PATH = $(CAMERA_PATH)/user/src/obj

CAMERA_INC := -I$(CAMERA_PATH)/user/include\
	-I$(COMM_INC)\

SRC_DIR := $(CAMERA_PATH)/user/src

LIB_A := $(LIB_PATH)/libi2cops.a
LIB_S := $(LIB_PATH)/libi2cops.so

CFLAGS += -Wall -fPIC

ifeq ($(EIGDB),EI_GDB)
CFLAGS += -g
endif

CFLAGS  += -O2
CFLAGS  += $(LIBS_CFLAGS)
DFLAGS  := -DEXT_REG
DFLAGS  += $(MEDIA_FLAGS)

DEPEND_FILE := $(foreach file,$(subst -I, ,$(CAMERA_INC)), $(wildcard $(file)/*.h))
SRCS_TMP := $(foreach file,$(subst -S, ,$(SRC_DIR)), $(notdir $(wildcard $(file)/*.c)))
SRCS = $(SRCS_TMP)

OBJS := $(SRCS:%.c=$(OBJ_PATH)/%.o)
VPATH = $(subst -S, ,$(SRC_DIR))

COMPILE = $(CC) $(CFLAGS) $(DFLAGS) $(CAMERA_INC) -lm

$(warning $(PARAM_FILE))
$(warning $(COMP_PARAM_FILE))
$(warning $(LIBS_LD_CFLAGS))

$(OBJ_PATH)/%.o : %.c $(DEPEND_FILE)
	@(echo "compiling $< ...")
	@[ -e $(LIB_PATH) ] || mkdir -p $(LIB_PATH)
	@[ -e $(OBJ_PATH) ] || mkdir -p $(OBJ_PATH)
	@($(COMPILE) -o $@ -c $<)

all:$(OBJS)
	@($(AR) -rcv $(LIB_A) $(OBJS))
	@($(CC) $(LIBS_LD_CFLAGS) -shared -fPIC -o $(LIB_S) $(OBJS))
	@mkdir -p $(INS_LIB)
	@cp $(LIB_S) $(INS_LIB)
	@cp $(LIB_A) $(INS_LIB)

	@for x in `find ./ -maxdepth 2 -mindepth 2 -name "Makefile" `; \
	   do cd `dirname $$x`; if [ $$? ]; then make || exit 1; cd ../; fi; done

clean:
	@$(RM) -rf $(LIB_A) $(LIB_S) $(OBJS)
	@$(RM) -rf $(LIB_PATH) $(OBJ_PATH)

	@for x in `find ./ -maxdepth 2 -mindepth 2 -name "Makefile" `; \
   do cd `dirname $$x`; if [ $$? ]; then make clean; cd ../; fi; done
